﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.WebControls;
using System.Drawing;


namespace SharePoint.Extensions.MOSSAnalytics
{
    public class GetTopQueriesWebPart : WebPart
    {
        protected override void RenderContents(System.Web.UI.HtmlTextWriter writer)
        {
            base.RenderContents(writer);
            this.EnsureChildControls();         

        }

        protected override void CreateChildControls()
        {

            try
            {
                GetTopQueries topqueries = new GetTopQueries();
                topqueries.NumberOfResults = 200;
                DataTable dtTopQueries = topqueries.GetData();

                SPGridView gvTopQueries = new SPGridView();
                gvTopQueries.AutoGenerateColumns = false;

                gvTopQueries.Columns.Add(Common.CreateBoundField("queryString", "Query"));
                gvTopQueries.Columns.Add(Common.CreateBoundField("scope", "Scope"));
                gvTopQueries.Columns.Add(Common.CreateBoundField("numQueries", "# of queries"));

                gvTopQueries.DataSource = dtTopQueries;
                gvTopQueries.DataBind();

                this.Controls.Add(gvTopQueries);
            }
            catch (Exception ex)
            {
                Label lblError = new Label();
                lblError.Text = ex.ToString();
                lblError.ForeColor = Color.Red;

                this.Controls.Add(lblError);
            }
        }

        
    }
}
